<template>
  <!-- tab基本信息表单  -->
  <el-scrollbar style="height: 100%;margin-top:10px;">
    <el-form
      ref="formRef"
      :model="formData"
      :rules="formRules"
      label-width="90px"
      class="form-input-m"
      inline-message
    >
      
      <el-form-item label="标本ID" prop="specimenId">
        <el-input v-model="formData.specimenId" placeholder="请输入标本ID" clearable></el-input>
      </el-form-item>
      <el-form-item label="标本类型" prop="specimenType">
        <el-input v-model="formData.specimenType" placeholder="请输入标本类型" clearable></el-input>
      </el-form-item>
      <el-form-item label="工作组" prop="workGroup">
        <el-input v-model="formData.workGroup" placeholder="请输入工作组" clearable></el-input>
      </el-form-item>
      <el-form-item label="操作人" prop="operator">
        <el-input v-model="formData.operator" placeholder="请输入操作人" clearable></el-input>
      </el-form-item>
      <el-form-item label="所在冰箱" prop="fridgeId">
        <el-input v-model="formData.fridgeId" placeholder="请输入所在冰箱" clearable></el-input>
      </el-form-item>
      <el-form-item label="所在层" prop="layerId">
        <el-input v-model="formData.layerId" placeholder="请输入所在层" clearable></el-input>
      </el-form-item>
      <el-form-item label="标本盒表" prop="specboxId">
        <el-input v-model="formData.specboxId" placeholder="请输入标本盒表" clearable></el-input>
      </el-form-item>
      <el-form-item label="库位号" prop="orderNumber">
        <el-input v-model="formData.orderNumber" placeholder="请输入库位号" clearable></el-input>
      </el-form-item>
      <el-form-item label="仓库位置" prop="location">
        <el-input v-model="formData.location" placeholder="请输入仓库位置" clearable></el-input>
      </el-form-item>
      <el-form-item label="检验时间" prop="inspectionTime">
        <el-input v-model="formData.inspectionTime" placeholder="请输入检验时间" clearable></el-input>
      </el-form-item>
      <el-form-item label="入库时间" prop="inTime">
        <el-input v-model="formData.inTime" placeholder="请输入入库时间" clearable></el-input>
      </el-form-item>
      <el-form-item label="存储到期时间" prop="expirationTime">
        <el-input v-model="formData.expirationTime" placeholder="请输入存储到期时间" clearable></el-input>
      </el-form-item>
      <el-form-item label="状态" prop="status">
        <el-input v-model="formData.status" placeholder="请输入状态" clearable></el-input>
      </el-form-item>
      <el-form-item label="所属科室" prop="deptName">
        <el-input v-model="formData.deptName" placeholder="请输入所属科室" clearable></el-input>
      </el-form-item>
      <el-form-item label="所属科室" prop="deptName">
        <SelectDataGrid :url="'/api/sys/getDepartmentList'" :column="[
                  { field: 'code', title: '科室编码' }, //列标题
                  { field: 'name', title: '科室名称' }
                ]" :searchParam="{ orgId }" :isPage="false" @SelectCellClick="
                  (e) => {
                    selectRow(e, rowIndex);
                  }
                " :width="'200px'">
        </SelectDataGrid>
      </el-form-item>
      <el-form-item label="" prop="">
        <el-button type="primary" @click="saveForm">保存</el-button>
      </el-form-item>
    </el-form>
  </el-scrollbar>
</template>

<script lang="ts" setup>
// 引入组件区 ==========
import {
  ref,
  reactive,
  watch,
  getCurrentInstance,
  PropType,
  onMounted
} from 'vue';
import { IsEmpty, formatDateTime } from '@/utils/common'; //导入工具函数
import { VxeMessage, VxeNotifyType } from '@/utils/CommonMessage';
import { funBdsJsResult } from '@/utils/Expression'; //函数公式
import SelectDataGrid from '@/components/SelectDataGrid.vue';
import { AppStore } from '@/stores/store';
import {  FormInstance } from 'element-plus';
import { 
samstoSampleStorageAdd, 
samstoSampleStorageEdit 
} from '@/views/lab_sys/samsto_set/apis/samsto_api';

// 全局变量定义区 ==========
let { proxy }: any = getCurrentInstance();
let store = AppStore();
let orgId = ref(null);
const formRef = ref<FormInstance>; //表单ref标记
// 表单绑定的数据类型限制
interface FormDataVO {
            id: string /*主键*/;
  	  			specimenId: String ;/*标本ID */
	    	  			specimenType: String ;/*标本类型 */
	    	  			workGroup: String ;/*工作组 */
	    	  			operator: String ;/*操作人 */
	    	  			fridgeId: String ;/*所在冰箱 */
	    	  			layerId: String ;/*所在层 */
	    	  			specboxId: String ;/*标本盒表 */
	    	  			orderNumber: String ;/*库位号 */
	    	  			location: String ;/*仓库位置 */
	    	  			inspectionTime: String ;/*检验时间 */
	    	  			inTime: String ;/*入库时间 */
	    	  			expirationTime: String ;/*存储到期时间 */
	    	  			status: Number ;/*状态 */
		  	  			deptName: String ;/*所属科室 */
	    }
// 表单绑定的数据
let formData = reactive<FormDataVO>({
            id: '',
      			specimenId: '' ,/*标本ID */
	      			specimenType: '' ,/*标本类型 */
	      			workGroup: '' ,/*工作组 */
	      			operator: '' ,/*操作人 */
	      			fridgeId: '' ,/*所在冰箱 */
	      			layerId: '' ,/*所在层 */
	      			specboxId: '' ,/*标本盒表 */
	      			orderNumber: '' ,/*库位号 */
	      			location: '' ,/*仓库位置 */
	      			inspectionTime: '' ,/*检验时间 */
	      			inTime: '' ,/*入库时间 */
	      			expirationTime: '' ,/*存储到期时间 */
	      			status: 1 ,/*状态 */
		      			deptName: '' ,/*所属科室 */
	  });
let types = reactive([]);
// 表单校验规则
const formRules: any = {
  specimenId:[{ required: true, message: '请输入标本ID' }], 
  specimenType:[{ required: true, message: '请输入标本类型' }], 
  workGroup:[{ required: true, message: '请输入工作组' }], 
  operator:[{ required: true, message: '请输入操作人' }], 
  fridgeId:[{ required: true, message: '请输入所在冰箱' }], 
  layerId:[{ required: true, message: '请输入所在层' }], 
  specboxId:[{ required: true, message: '请输入标本盒表' }], 
  orderNumber:[{ required: true, message: '请输入库位号' }], 
  location:[{ required: true, message: '请输入仓库位置' }], 
  inspectionTime:[{ required: true, message: '请输入检验时间' }], 
  inTime:[{ required: true, message: '请输入入库时间' }], 
  expirationTime:[{ required: true, message: '请输入存储到期时间' }], 
  status:[{ required: true, message: '请输入状态' }], 
  deptName:[{ required: true, message: '请输入所属科室' }], 
};

const EditExtraPropertiesRef = ref(null); //配置扩展属性弹框ref

defineExpose({
  addNew,
  saveForm
});

// 父组件传值
let props = defineProps({
  currentRow: { type: Object as PropType<any>, required: true }
}) as any;
watch(
  () => props.currentRow,
  (nval, oval) => {
    Object.assign(formData, nval);
  },
  {
    deep: true,
    immediate: true
  }
);


onMounted(() => {
  orgId = store.userInfo.orgId;
  //formData.bankNumber=generateCode();
});


// 方法定义区 =============
//生成题库编码
function generateCode() {
  let now = new Date();
  console.log(now,"now");
  let y = now.getFullYear(),
    m = now.getMonth() + 1,
    d = now.getDate(),
    h = now.getHours(),
    mm = now.getMinutes(),
    s = now.getSeconds(),
    ms = now.getMilliseconds();
  return "TM"+y + m+d+h+mm+s+ms;
}
function addNew(callback: any) {
  clear();
  if (callback) {
    callback({ a: '初始化完成' });
  }
}
// 清空表单
function clear() {
  //formData.startus = 1;
  //formData.bankNumber = generateCode();
  //formData.seq = 1;
  formData.id = "";
    			formData.specimenId= '' ;/*标本ID */
	    			formData.specimenType= '' ;/*标本类型 */
	    			formData.workGroup= '' ;/*工作组 */
	    			formData.operator= '' ;/*操作人 */
	    			formData.fridgeId= '' ;/*所在冰箱 */
	    			formData.layerId= '' ;/*所在层 */
	    			formData.specboxId= '' ;/*标本盒表 */
	    			formData.orderNumber= '' ;/*库位号 */
	    			formData.location= '' ;/*仓库位置 */
	    			formData.inspectionTime= '' ;/*检验时间 */
	    			formData.inTime= '' ;/*入库时间 */
	    			formData.expirationTime= '' ;/*存储到期时间 */
	    			formData.status= 1 ;/*状态 */
		    			formData.deptName= '' ;/*所属科室 */
	}

// 保存信息
function saveForm() {
  submitForm();
}

// 保存
const submitForm = () => {
  let elInfo = proxy.$refs.formRef;
  elInfo.validate(async (valid: any) => {
    if (!valid) {
      return false;
    }
    let res: any;
    if (IsEmpty(formData.id)) {
      res = await samstoSampleStorageAdd(formData);
    } else {
      res = await samstoSampleStorageEdit(formData);
    }
    res.status.code === 1 ? VxeMessage(res.status.msg, VxeNotifyType.success) : VxeMessage(res.status.msg, VxeNotifyType.error);
    clear();
    proxy.$emit('findList');
  });
};
function selectRow(e, rowIndex) {
  if (!IsEmpty(e)) {
    formData.deptName = e.code;
  }
};
const states = [
  {
    value: 1,
    label: '启用'
  },
  {
    value: 0,
    label: '停用'
  }
];
</script>

<style lang="scss" scoped>
// 中等尺寸时 配置扩展属性按钮
.el-button--default {
  padding: 5px 11px;
}
</style>

